package com.cash.lemoloan.networklib.Utils;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.cash.lemoloan.networklib.Intetface.ResultInterface;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import okhttp3.Call;
import okhttp3.MediaType;


public class NetWorkUtils {

    private static final String TAG = "NetWorkUtils";

    /**
     * 改版网络请求,传参json
     **/
    public static void postJsonOkData(Context context, String json, final String url, final ResultInterface resultInterface) {

        try {
            postJsonMethod(context, url, json, new StringCallback() {

                @Override
                public String parseNetworkResponse(okhttp3.Response response, int id) throws IOException {
                    return super.parseNetworkResponse(response, id);
                }

                @Override
                public void onError(Call call, Exception e, int id) {
                    resultInterface.onError(e.getMessage());
                }

                @Override
                public void onResponse(String msg, int id) {
                    resultInterface.onSuccess(msg, id);
                }

            });

        } catch (Exception e) {
        }
    }

    /**
     * get请求
     */
    public static void getJsonOkData(Context context, final Map<String, String> args, final String url, final ResultInterface resultInterface) {
        try {

            NetWorkUtils.GetMethod(context, url, args, new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int id) {
                    //isShowNetError = true;
                    resultInterface.onError(e.getMessage());
                }

                @Override
                public void onResponse(String response, int id) {
                    resultInterface.onSuccess(response, id);
                }
            });

        } catch (Exception e) {
        }
    }


    /**
     * post请求封装,传参json数据
     **/
    public static void postJsonMethod(Context context, String url, String json, StringCallback callback) {

        OkHttpUtils
                .postString()
                .tag(context)
                .url(url)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .headers(getHeaders(context))
                .build()
                .execute(callback);
    }

    public static void postPhotoMethod(Context context, String url, String json, StringCallback callback) {
        OkHttpUtils
                .postString()
                .tag(context)
                .url(url)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .headers(getHeaders(context))
                .build()
                .execute(callback);
    }

    /**
     * get请求封装
     **/
    public static void GetMethod(Context context, String url, Map<String, String> map, StringCallback callback) {

        OkHttpUtils
                .get()
                .url(url)
                .params(map)
                .headers(getHeaders(context))
                .build()
                .execute(callback);
    }

    private static Map getHeaders(Context context) {

        HashMap<String, String> headers = new HashMap<>();
        return headers;
    }

//    public static void downloadFace(Context context, String url) {
//        if (!TextUtils.isEmpty(url)) {
//            // 创建临时路径存储模型
//            File dir = context.getDir("models", Context.MODE_PRIVATE);
//            try {
//                if (!dir.exists()) {
//                    dir.mkdirs();
//                }
//                String fileName = "face.zip";
//                String path = dir.getAbsolutePath() + File.separator;
//                if (new File(path, fileName).exists()) {
//                    //文件已存在
//                    return;
//                }
//                //okHttp下载url文件并解压到path路径下
//                OkHttpUtils.get().url(url).build().execute(new FileCallBack(path, fileName) {
//                    @Override
//                    public void onError(Call call, Exception e, int i) {
//                        Log.e(TAG, "下载失败" + e.getMessage());
//                    }
//
//                    @Override
//                    public void onResponse(File file, int i) {
//                        //解压file到当前目录
//                        try {
//                            unzip(file.getAbsolutePath(), dir.getAbsolutePath());
//                        } catch (IOException e) {
//                            Log.e(TAG, "解压异常" + e.getMessage());
//                            file.delete();
//                        }
//                    }
//                });
//            } catch (Exception e) {
//                Log.e(TAG, "下载异常" + e.getMessage());
//            }
//        }
//    }


//    private static void unzip(String zipFilePath, String destDirectory) throws IOException {
//        File destDir = new File(destDirectory);
//        if (!destDir.exists()) {
//            destDir.mkdir();
//        }
//        ZipInputStream zipIn = new ZipInputStream(new FileInputStream(zipFilePath));
//        ZipEntry entry = zipIn.getNextEntry();
//        while (entry != null) {
//            String filePath = destDirectory + File.separator + entry.getName();
//            if (!entry.isDirectory()) {
//                extractFile(zipIn, filePath);
//            } else {
//                File dir = new File(filePath);
//                dir.mkdir();
//            }
//            zipIn.closeEntry();
//            entry = zipIn.getNextEntry();
//        }
//        zipIn.close();
//    }

//    private static void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
//        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
//        byte[] bytesIn = new byte[4096];
//        int read;
//        while ((read = zipIn.read(bytesIn)) != -1) {
//            bos.write(bytesIn, 0, read);
//        }
//        bos.close();
//    }

}
